import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*; 


public class bruteforce {
        
public void mapReduce(String WORD_FILE,String[] RAW_FILE) throws IOException{
                
                
                PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
                
                long time1 = System.currentTimeMillis();
                long time2;

                //System.out.println(RAW_FILE.length);
                for(int num = 0;num<RAW_FILE.length;num++){
                        
                        Scanner linereader = new Scanner( new File(WORD_FILE));
                        
                        String KeyWord;
                        int Loc =0;
                        boolean matched;
                        
                        File file = new File(RAW_FILE[num]);
                        byte[] longByte = new byte[(int)file.length()];
                        FileInputStream fileInput = new FileInputStream(file);
                        fileInput.read(longByte);

                        while(linereader.hasNext()){
                                
                                time2 = System.currentTimeMillis();
                                System.out.print((time2-time1) + "  ");
                                
                                KeyWord = linereader.next();
                                System.out.println("Word => "+KeyWord);
                                writer.printf(RAW_FILE[num]+","+KeyWord);
                                byte[] keyByte = new byte[KeyWord.length()];
                                keyByte = KeyWord.getBytes();
                                //System.out.println("Loc = "+Loc+" Key = "+keyByte.length+" Str = "+longByte.length);
                                
                        for(Loc = 0;Loc<(longByte.length-(keyByte.length-1));Loc++){ 
                                
                                if(longByte[Loc]==keyByte[0]){
                                        matched = false;
                                        
                                        for(int i=1;i<keyByte.length;i++){
                                                //System.out.print(" "+i);
                                                if(longByte[Loc+i]==keyByte[i]){
                                                        matched = true;
                                                }else{
                                                        matched = false;
                                                        break;
                                                }
                                        }
                                        //System.out.println();
                                        if(matched){
                                                writer.printf(","+Loc);
                                        }
                                }
                        }
                        
                        //System.out.println("Loc = "+Loc);
                     
                     writer.println();
                 }
                        
                        
                        fileInput.close();
                        linereader.close();
                }
                writer.close();
                
                time2 = System.currentTimeMillis();
                System.out.print((time2-time1) + "  ");
        }
        
        public static void main(String[] args) throws IOException {
                bruteforce boot = new bruteforce();

                String[] raw = {"2.dat"};
                
                long time1 = System.currentTimeMillis();
                long time2;
                
                System.out.println("Running MapReduce....");
                boot.mapReduce("query.dat",raw);
                
                time2 = System.currentTimeMillis();
                
                System.out.print("Finished => ");
                System.out.print((time2-time1)/60000 + ":"+ ((time2-time1)%60000)/1000);
    }
}